package com.ddwl.goods.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.DiscountItemSkuMapper;
import com.ddwl.goods.model.DiscountItemSku;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @author hemiaojun
 */
@Repository
public class DiscountItemSkuDao extends TkDao<DiscountItemSkuMapper, DiscountItemSku> {

    @Resource
    private DiscountItemSkuMapper discountItemSkuMapper;

    public List<DiscountItemSku> findByDiscountItemId(String id) {
        DiscountItemSku dis = new DiscountItemSku();
        dis.setDiscountItemId(id);
        dis.setSkuStatus(BaseEnum.StatusEnum.ENABLE.getCode());
        return this.select(dis);
    }

    public void deleteByItemId(String itemId) {
        Example example = getExample();
        example.and().andEqualTo("itemId", itemId);
        DiscountItemSku dis = new DiscountItemSku();
        dis.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        dis.setGmtModified(Instant.now());
        discountItemSkuMapper.updateByExampleSelective(dis, example);
    }

    public List<DiscountItemSku> findListByBarcodes(List<String> barcodes) {
        Example example = getExample();
        example.and().andIn("barcode", barcodes)
                .andEqualTo("skuStatus", BaseEnum.StatusEnum.ENABLE.getCode());
        return discountItemSkuMapper.selectByExample(example);
    }

    public void batchUpdateStock(List<DiscountItemSku> list) {
        discountItemSkuMapper.batchUpdateStock(list);
    }

    public void updateDiscountItemSkuStatus(String id, Integer disable) {
        Example example = getExample();
        example.and().andEqualTo("discountItemId", id);
        DiscountItemSku dis = new DiscountItemSku();
        dis.setSkuStatus(disable);
        dis.setGmtModified(Instant.now());
        discountItemSkuMapper.updateByExampleSelective(dis, example);
    }
}